﻿using DLL.NET50.DB.PIVAS.Entitys.DTO;
using DLL.NET50.DB.PIVAS.ORM.Service;
using DLL.NET60.DB.MS.Entity.DTO;
using DLL.Standard.Infrastructure;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Regions;
using DLL.NET70.Infrastructure.Prism;
using DLL.NET70.Infrastructure.Prism.MVVM;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows;
using DLL.NET.DB.MS.Drugs.CRUD.Service;
using DLL.Standard.Infrastructure.Log;
using DLL.Standard.Infrastructure.Entity;

namespace PrismModule.EditDrugs.ViewModels
{
    public class PUC_EditDrugViewModel : CMVVMBase, IRegionMemberLifetime
    {
        #region property variable
        private DTO_adi_dic_drug _msDrug;

        public DTO_adi_dic_drug msDrug
        {
            get { return _msDrug; }
            set { SetProperty(ref _msDrug, value); }
        }

        private List<DTO_adi_FlushGroup> _msFlushGroups;

        public List<DTO_adi_FlushGroup> msFlushGroups
        {
            get { return _msFlushGroups; }
            set { SetProperty(ref _msFlushGroups, value); }
        }

        private DTO_adi_FlushGroup _msFlushGroup;

        public DTO_adi_FlushGroup msFlushGroup
        {
            get { return _msFlushGroup; }
            set { SetProperty(ref _msFlushGroup, value); }
        }

        private List<DTO_adi_FlushParam> _msFlushParams;

        public List<DTO_adi_FlushParam> msFlushParams
        {
            get { return _msFlushParams; }
            set { SetProperty(ref _msFlushParams, value); }
        }

        private DTO_adi_FlushParam _msFlushParam;

        public DTO_adi_FlushParam msFlushParam
        {
            get { return _msFlushParam; }
            set { SetProperty(ref _msFlushParam, value); }
        }

        private DTO_adi_PumpBackParam _pumpBack;

        public DTO_adi_PumpBackParam pumpBackParam
        {
            get { return _pumpBack; }
            set { SetProperty(ref _pumpBack, value); }
        }

        private int _flushLoopIndex = 0;

        public int flushLoopIndex
        {
            get { return _flushLoopIndex; }
            set { SetProperty(ref _flushLoopIndex, value); }
        }

        private int _flushGroupIndex = 0;

        public int flushGroupIndex
        {
            get { return _flushGroupIndex; }
            set { SetProperty(ref _flushGroupIndex, value); }
        }

        private int _pumpBackLoopIndex = 0;

        public int PumpBackLoopIndex
        {
            get { return _pumpBackLoopIndex; }
            set { SetProperty(ref _pumpBackLoopIndex, value); }
        }

        private DTO_adi_dic_drug _oldDicDrug = new DTO_adi_dic_drug();

        public DTO_adi_dic_drug oldDicDrug
        {
            get { return _oldDicDrug; }
            set { SetProperty(ref _oldDicDrug, value); }
        }

        private List<DTO_adi_dic_drug> _LocalDrugs = new List<DTO_adi_dic_drug>();

        public List<DTO_adi_dic_drug> LocalDrugs
        {
            get { return _LocalDrugs; }
            set { SetProperty(ref _LocalDrugs, value); }
        }

        //SelectUsedDrug
        private DTO_adi_dic_drug _selectedLocalDrug = new DTO_adi_dic_drug();

        public DTO_adi_dic_drug SelectedLocalDrug
        {
            get { return _selectedLocalDrug; }
            set { SetProperty(ref _selectedLocalDrug, value); }
        }

        private List<DTO_pivas_read_drugs> _PivasDrugs = new List<DTO_pivas_read_drugs>();

        public List<DTO_pivas_read_drugs> PivasDrugs
        {
            get { return _PivasDrugs; }
            set { SetProperty(ref _PivasDrugs, value); }
        }

        //SelectUsedDrug
        private DTO_pivas_read_drugs _selectedPivasDrug = new DTO_pivas_read_drugs();

        public DTO_pivas_read_drugs SelectedPivasDrug
        {
            get { return _selectedPivasDrug; }
            set { SetProperty(ref _selectedPivasDrug, value); }
        }

        private string _pyCode = "";

        public string pyCode
        {
            get { return _pyCode; }
            set { SetProperty(ref _pyCode, value); }
        }

        //ApprovalNumber
        private string _ApprovalNumber = "";

        public string ApprovalNumber
        {
            get { return _ApprovalNumber; }
            set { SetProperty(ref _ApprovalNumber, value); }
        }

        //pivaspyCode
        private string _pivaspyCode = "";

        public string pivaspyCode
        {
            get { return _pivaspyCode; }
            set { SetProperty(ref _pivaspyCode, value); }
        }

        private string _drugCode = "";

        public string drugCode
        {
            get { return _drugCode; }
            set { SetProperty(ref _drugCode, value); }
        }

        //drugDose
        private string _drugDose = "";

        public string drugDose
        {
            get { return _drugDose; }
            set { SetProperty(ref _drugDose, value); }
        }

        private string _pivasDrugDose = "";

        public string pivasDrugDose
        {
            get { return _pivasDrugDose; }
            set { SetProperty(ref _pivasDrugDose, value); }
        }

        //mafcName
        private string _mafcName = "";

        public string mafcName
        {
            get { return _mafcName; }
            set { SetProperty(ref _mafcName, value); }
        }

        private bool _updateFlag = false;

        public bool updateFlag
        {
            get { return _updateFlag; }
            set { SetProperty(ref _updateFlag, value); }
        }

        #endregion property variable

        #region navigate overide

        public bool KeepAlive
        {
            get
            {
                //return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
                return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
            }
        }

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            try
            {
                journal = navigationContext.NavigationService.Journal;
                GoForwardCommand.RaiseCanExecuteChanged();
                GoBackCommand.RaiseCanExecuteChanged();
                var drug = navigationContext.Parameters["adidrug"] as DTO_adi_dic_drug;
                if (drug != null)
                {
                    msDrug = drug;
                    if (msDrug.MAFC_NAME.Length == 0)
                    {
                        msDrug.MAFC_NAME = "厂家名称";
                    }
                }
                else
                {
                    Initialize();
                }
                msFlushGroups = msDrug.flush_Groups;

                msFlushGroup = msFlushGroups[0];
                flushGroupIndex = msFlushGroup.FlushGroupID;

                msFlushParam = msFlushGroup.flush_Params[0];
                pumpBackParam = msFlushGroup.pumpBack_Params[0];
                flushLoopIndex = msFlushParam.LoopIndex;
                PumpBackLoopIndex = pumpBackParam.LoopIndex;
            }
            catch (Exception e)
            {
                LogHelper.Warn("进入药品编辑界面，出现异常：" + e.Message);
            }
        }

        #endregion navigate overide

        #region window load

        public override void Loaded()
        {
            try
            {
                LogMessage("PUC_EditDrugViewModel  初始化");
                System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                stopwatch.Start(); //  开始监视代码
                LocalDrugs = _localDrugService.GetDrugs();
                PivasDrugs = _pivasDrugsService.GetDrugs();
                LogMessage("用时=" + GetUsedTotalTime(stopwatch).ToString() + "ms");
                base.Loaded();
            }
            catch (Exception e)
            {
                LogMessage("药品编辑：Loaded()出现异常：" + e.Message, LogLevel.Warning);
            }
        }

        public override void UnLoaded()
        {
            base.UnLoaded();
        }

        #endregion window load

        #region interface variable
        private IContainerExtension _container;
        private ILocalDrugsService _localDrugService;
        private IMSDrugsService _msDrugsService;
        private IPivasDrugsService _pivasDrugsService;
        private IEventAggregator _eventAggregator;
        #endregion interface variable

        public PUC_EditDrugViewModel(IRegionManager regionManager,
            IEventAggregator eventAggregator,
            IApplicationCommands applicationCommands,
            IContainerExtension container,
            ILocalDrugsService localDrugsService,
            IMSDrugsService msDrugsService,
            IPivasDrugsService pivasDrugsService) :
            base(regionManager, eventAggregator, applicationCommands)
        {
            _eventAggregator = eventAggregator;

            InitializeCommand = new DelegateCommand(Initialize);
            _container = container;
            _localDrugService = localDrugsService;
            _msDrugsService = msDrugsService;
            _pivasDrugsService = pivasDrugsService;
            RefreshCommand = new DelegateCommand(Refresh);
            SearchCommand = new DelegateCommand(Search);
            LoadDrugCommand = new DelegateCommand(LoadDrug);

            PivasSearchCommand = new DelegateCommand(PivasSearch);
            PivasLoadDrugCommand = new DelegateCommand(PivasLoadDrug);
            PivasDrugs_ComboxNSICCommand = new DelegateCommand(PivasDrugChanged);
            LocalDrugs_ComboxNSICCommand = new DelegateCommand(LocalDrugChanged);
            FlushLoopIndex_ComboxNSICCommand = new DelegateCommand(FlushLoopIndexChange);
            SOLUBILITY_ComboxNSICCommand = new DelegateCommand(SOLUBILITY_ComboxSIC);
            PumpBackLoopIndex_ComboxNSICCommand = new DelegateCommand(PumpBackLoopIndexChange);
            FlushGroupIndex_ComboxNSICCommand = new DelegateCommand(FlushGroupIndexChange);
        }

        #region function

        public override void Save()
        {
            try
            {
                bool ret = false;
                LogMessage("开始保存药品");
                msDrug.flush_Groups[flushGroupIndex] = msFlushGroup;
                //第一步，判断是不是所有的属性都设置了
                if (false == PropertyIsSet(msDrug))
                {
                    LogMessage("有的药品属性没有设置，请将属性参数设置完成后再保存！");
                    return;
                }

                string uniqueCode = msDrug.GetUniqueCode(); ;
                msDrug.Drug_UniqueCode = uniqueCode;
                msDrug.SetFlushParamUniqueCode();
                msDrug.updateTime = DateTime.Now;
                ret = _msDrugsService.MS_SaveDrug(msDrug);
                if (false == ret)
                {
                    Message = "数据保存到usedDrug失败！可能本地存在药品名称，厂家名称，计量都相同的药";
                    LogMessage(Message, LogLevel.Warning);
                }
                else
                {
                    Message = "数据保存到usedDrug成功！";
                    LogMessage(Message);
                }

                //先判断 该药品编码是否存在
                ret = _localDrugService.SaveDrugs(msDrug);
                if (false == ret)
                {
                    Message = "数据保存到dicDrug失败！";
                    LogMessage(Message, LogLevel.Warning);
                }
                else
                {
                    SendCommandMessage(GlobalCommand.SaveOK);
                    Message = "数据保存到dicDrug成功！";
                    LogMessage(Message);
                }
                if (true == ret)
                {
                    base.GoBack();
                }
            }
            catch (Exception e)
            {
                LogMessage("保存药品出现异常：" + e.Message, LogLevel.Warning);
            }
        }

        private bool PropertyIsSet(DTO_adi_dic_drug drug)
        {
            if (drug.MAFC_NAME.Length == 0)
            {
                drug.MAFC_NAME = "厂家名称";
            }
            if ((drug.SOLVENT_FLAG == null) ||
                (drug.NEEDLE_TYPE == null) ||
                (drug.DRUG_NAME.Length == 0) ||
                (drug.MAFC_NAME.Length == 0) ||
                (drug.DOSE <= 0) ||
                (drug.DRUG_CODE.Length == 0) ||
                (drug.PY_CODE.Length == 0))
            {
                return false;
            }
            return true;
        }

        public DelegateCommand InitializeCommand { get; set; }

        private void Initialize()
        {
            try
            {
                DTO_adi_dic_drug dicDrug = new DTO_adi_dic_drug();
                if (dicDrug.MAFC_NAME.Length == 0)
                {
                    dicDrug.MAFC_NAME = "厂家名称";
                }
                dicDrug.InitializeFlushParam();
                msDrug = dicDrug;
            }
            catch (Exception e)
            {
                LogHelper.Warn("初始化药品出现异常：" + e.Message);
            }
        }

        public DelegateCommand SearchCommand { get; set; }

        private void Search()
        {
            try
            {
                if (pyCode.Length > 0)
                {
                    LocalDrugs = _msDrugsService.SearchDrugsByPyCode(pyCode);
                }
                else if (ApprovalNumber.Length > 0)
                {
                    LocalDrugs = _msDrugsService.SearchDrugsByApprovatNumber(ApprovalNumber);
                }
            }
            catch (Exception e)
            {
                LogHelper.Warn("查询出现异常：" + e.Message);
            }
        }

        public DelegateCommand RefreshCommand { get; set; }

        private void Refresh()
        {
            try
            {
                LocalDrugs = _msDrugsService.GetDrugs();
            }
            catch (Exception e)
            {
                LogHelper.Warn("跟新出现异常：" + e.Message);
            }
        }

        public DelegateCommand LoadDrugCommand { get; set; }

        private void LoadDrug()
        {
            try
            {
                if (SelectedLocalDrug != null)
                {
                    DTO_pivas_read_drugs pd = DicDrugToPivasDrug(msDrug);
                    msDrug = (DTO_adi_dic_drug)SelectedLocalDrug.Clone();

                    msDrug = PivasCopyToDicDrug(pd, msDrug);

                    msFlushGroups = msDrug.flush_Groups;

                    msFlushGroup = msFlushGroups[0];
                    flushGroupIndex = msFlushGroup.FlushGroupID;

                    msFlushParam = msFlushGroup.flush_Params[0];
                    pumpBackParam = msFlushGroup.pumpBack_Params[0];
                    flushLoopIndex = msFlushParam.LoopIndex;
                    PumpBackLoopIndex = pumpBackParam.LoopIndex;
                }
            }
            catch (Exception e)
            {
                LogHelper.Warn("加载药品出现异常：" + e.Message);
            }
        }

        public DelegateCommand PivasSearchCommand { get; set; }

        private void PivasSearch()
        {
            try
            {
                PivasDrugs = _pivasDrugsService.GetDrugInfo(drugCode, pivaspyCode, "");
            }
            catch (Exception e)
            {
                LogHelper.Warn("PIVAS查询出现异常：" + e.Message);
            }
        }

        public DelegateCommand PivasLoadDrugCommand { get; set; }

        private void PivasLoadDrug()
        {
            try
            {
                if (SelectedPivasDrug != null)
                {
                    _updateFlag = false;
                    //添加判断，该药是不是已经存在了，如果存在了，就不能导入了
                    string drugcode = SelectedPivasDrug.DRUG_CODE;
                    List<DTO_adi_dic_drug> ld = _msDrugsService.SearchDrugsByDrugCode(drugcode);
                    if (0 == ld.Count)
                    {
                        //msrobot库不存在该药品编码
                        string uniqueCode = SelectedPivasDrug.GetUniqueCode();
                        List<DTO_adi_dic_drug> mld = _msDrugsService.SearchByUniqueCode(uniqueCode);
                        if (mld.Count == 1)
                        {
                            //msrobot存在，相同的药，但是药品编码相同
                            msDrug = mld[0];
                            msDrug = PivasCopyToDicDrug(SelectedPivasDrug, msDrug);
                            _updateFlag = true;
                        }
                        else
                        {
                            //msrobot 不存在相同的药
                            List<DTO_adi_dic_drug> sld = _localDrugService.SearchByUniqueCode(uniqueCode);
                            if (sld.Count == 1)
                            {
                                //读取savedDrug里面存在药，直接加载过来

                                msDrug = PivasCopyToDicDrug(SelectedPivasDrug, sld[0]);
                            }
                            else
                            {
                                //saveDrugs库里面没有相同的药
                                msDrug = PivasCopyToDicDrug(SelectedPivasDrug, msDrug);
                            }
                        }
                        msFlushGroups = msDrug.flush_Groups;

                        msFlushGroup = msFlushGroups[0];
                        flushGroupIndex = msFlushGroup.FlushGroupID;

                        msFlushParam = msFlushGroup.flush_Params[0];
                        pumpBackParam = msFlushGroup.pumpBack_Params[0];
                        flushLoopIndex = msFlushParam.LoopIndex;
                        PumpBackLoopIndex = pumpBackParam.LoopIndex;
                    }
                    else
                    {
                        MessageBox.Show("该药品编码已经存在，无法导入");
                    }
                }
            }
            catch (Exception e)
            {
                LogHelper.Warn("Pivas加载出现异常：" + e.Message);
            }
        }

        private DTO_pivas_read_drugs DicDrugToPivasDrug(DTO_adi_dic_drug dicDrug)
        {
            DTO_pivas_read_drugs dstDrug = new DTO_pivas_read_drugs();
            try
            {
                if (dicDrug.MAFC_NAME.Length == 0)
                {
                    dicDrug.MAFC_NAME = "厂家名称";
                }
                dstDrug.DRUG_CODE = dicDrug.DRUG_CODE;
                dstDrug.DRUG_NAME = dicDrug.DRUG_NAME;
                dstDrug.DOSE = Convert.ToDecimal(dicDrug.DOSE);
                dstDrug.SPECIFICATION = dicDrug.SPECIFICATION;
                dstDrug.DOSE_UNIT = dicDrug.DOSE_UNIT;
                dstDrug.MAFC_NAME = dicDrug.MAFC_NAME;
                dstDrug.PY_CODE = dicDrug.PY_CODE;
            }
            catch (Exception e)
            {
                LogHelper.Warn("dicToPivas出现异常：" + e.Message);
            }
            return dstDrug;
        }

        public DTO_adi_dic_drug PivasCopyToDicDrug(DTO_pivas_read_drugs newPivasDrug, DTO_adi_dic_drug oldDicDrug)
        {
            DTO_adi_dic_drug dstDrug = (DTO_adi_dic_drug)oldDicDrug.Clone();
            try
            {
                if (newPivasDrug.MAFC_NAME.Length == 0)
                {
                    newPivasDrug.MAFC_NAME = "厂家名称";
                }
                dstDrug.DRUG_CODE = newPivasDrug.DRUG_CODE;
                dstDrug.DRUG_NAME = newPivasDrug.DRUG_NAME;
                dstDrug.DOSE = (float)newPivasDrug.DOSE;
                dstDrug.SPECIFICATION = newPivasDrug.SPECIFICATION;
                dstDrug.DOSE_UNIT = newPivasDrug.DOSE_UNIT;
                dstDrug.MAFC_NAME = newPivasDrug.MAFC_NAME;
                dstDrug.PY_CODE = newPivasDrug.PY_CODE;
                dstDrug.Drug_UniqueCode = dstDrug.GetUniqueCode();
                dstDrug.SetFlushParamUniqueCode();
                //dstDrug.ApprovalNumber = newPivasDrug.APPROVAL_NUMBER;
            }
            catch (Exception e)
            {
                LogHelper.Warn("pivasToDic出现异常：" + e.Message);
            }

            return dstDrug;
        }

        #endregion function

        #region 记录药品字典改变信息

        public void Write1(string path, string str)
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.Write(str);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }

        #region 判断两个类是否相等的方法

        /// <summary>
        /// 比较--两个类型一样的实体类对象的值
        /// </summary>
        /// <param name="oneT"></param>
        /// <returns></returns>
        private bool CompareType<T>(T oneT, T twoT)
        {
            #region
            bool result = true;//两个类型作比较时使用,如果有不一样的就false
            Type typeOne = oneT.GetType();
            Type typeTwo = twoT.GetType();
            //如果两个T类型不一样  就不作比较
            if (!typeOne.Equals(typeTwo)) { return false; }
            PropertyInfo[] pisOne = typeOne.GetProperties(); //获取所有公共属性(Public)
            PropertyInfo[] pisTwo = typeTwo.GetProperties();
            //如果长度为0返回false
            if (pisOne.Length <= 0 || pisTwo.Length <= 0)
            {
                return false;
            }
            //如果长度不一样，返回false
            if (!(pisOne.Length.Equals(pisTwo.Length))) { return false; }
            //遍历两个T类型，遍历属性，并作比较
            for (int i = 0; i < pisOne.Length; i++)
            {
                //获取属性名
                string oneName = pisOne[i].Name;
                string twoName = pisTwo[i].Name;
                //获取属性的值
                object oneValue = pisOne[i].GetValue(oneT, null);
                object twoValue = pisTwo[i].GetValue(twoT, null);
                //比较,只比较值类型
                if ((pisOne[i].PropertyType.IsValueType || pisOne[i].PropertyType.Name.StartsWith("String")) && (pisTwo

                [i].PropertyType.IsValueType || pisTwo[i].PropertyType.Name.StartsWith("String")))
                {
                    if (oneName.Equals(twoName))
                    {
                        if (oneValue == null)
                        {
                            if (twoValue != null)
                            {
                                result = false;
                                break; //如果有不一样的就退出循环
                            }
                        }
                        else if (oneValue != null)
                        {
                            if (twoValue != null)
                            {
                                if (!oneValue.Equals(twoValue))
                                {
                                    result = false;
                                    break; //如果有不一样的就退出循环
                                }
                            }
                            else if (twoValue == null)
                            {
                                result = false;
                                break; //如果有不一样的就退出循环
                            }
                        }
                    }
                    else
                    {
                        result = false;
                        break;
                    }
                }
                else
                {
                    //如果对象中的属性是实体类对象，递归遍历比较
                    bool b = CompareType(oneValue, twoValue);
                    if (!b) { result = b; break; }
                }
            }
            return result;
            #endregion 判断两个类是否相等的方法
        }

        private string CompareTypeAndGetChanged<T>(T oneT, T twoT)
        {
            string str = "";
            #region
            //bool result = true;//两个类型作比较时使用,如果有不一样的就false
            Type typeOne = oneT.GetType();
            Type typeTwo = twoT.GetType();
            //如果两个T类型不一样  就不作比较
            if (!typeOne.Equals(typeTwo)) { return str; }
            PropertyInfo[] pisOne = typeOne.GetProperties(); //获取所有公共属性(Public)
            PropertyInfo[] pisTwo = typeTwo.GetProperties();
            //如果长度为0返回false
            if (pisOne.Length <= 0 || pisTwo.Length <= 0)
            {
                return str;
            }
            //如果长度不一样，返回false
            if (!(pisOne.Length.Equals(pisTwo.Length))) { return str; }
            //遍历两个T类型，遍历属性，并作比较
            for (int i = 0; i < pisOne.Length; i++)
            {
                //获取属性名
                string oneName = pisOne[i].Name;
                string twoName = pisTwo[i].Name;
                //获取属性的值
                object oneValue = pisOne[i].GetValue(oneT, null);
                object twoValue = pisTwo[i].GetValue(twoT, null);
                //比较,只比较值类型
                if ((pisOne[i].PropertyType.IsValueType || pisOne[i].PropertyType.Name.StartsWith("String")) && (pisTwo

                [i].PropertyType.IsValueType || pisTwo[i].PropertyType.Name.StartsWith("String")))
                {
                    if (oneName.Equals(twoName))
                    {
                        if (oneValue == null)
                        {
                            if (twoValue != null)
                            {
                                //result = false;
                                str = str + "字段名：" + oneName + "   " + "修改前数值=null;  修改后数值=" + twoValue.ToString() + "\r\n";
                                //break; //如果有不一样的就退出循环
                            }
                        }
                        else if (oneValue != null)
                        {
                            if (twoValue != null)
                            {
                                if (!oneValue.Equals(twoValue))
                                {
                                    //result = false;
                                    str = str + "字段名：" + oneName + "   " + "修改前数值=" + oneValue.ToString() + ";  修改后数值=" + twoValue.ToString() + "\r\n";
                                    //break; //如果有不一样的就退出循环
                                }
                            }
                            else if (twoValue == null)
                            {
                                //result = false;
                                str = str + "字段名：" + oneName + "   " + "修改前数值=" + oneValue.ToString() + ";  修改后数值=null" + "\r\n";
                                //break; //如果有不一样的就退出循环
                            }
                        }
                    }
                    else
                    {
                        //result = false;
                        //break;
                    }
                }
                else
                {
                    //如果对象中的属性是实体类对象，递归遍历比较
                    string b = CompareTypeAndGetChanged(oneValue, twoValue);
                    if (b.Length > 0)
                    {
                        str = str + b + "/r/n";
                        //break;
                    }
                }
            }
            return str;
            #endregion 记录药品字典改变信息
        }

        private void LogDrugPropertyChange<DTO_adi_dic_drug>(DTO_adi_dic_drug olddrug, DTO_adi_dic_drug newdrug)
        {
            try
            {
                string restr = CompareTypeAndGetChanged<DTO_adi_dic_drug>(olddrug, newdrug);
                string ct = DateTime.Now.ToString("f");
                string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
                //string name = currentDicDrug.DRUG_NAME.Replace(@"/","");
                //只保留字母、数字 和汉字

                string name = Regex.Replace(msDrug.DRUG_NAME, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", "");
                string filePath = str_1 + @"SavedDrugs\" + name + "-" + (Convert.ToDouble(msDrug.DOSE)).ToString() + ".txt";
                //判断文件是否存在
                //FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
                //StreamWriter sw;
                if (restr.Length == 0)
                {
                    return;
                }
                string str = "修改日期：" + ct + "\r\n" + restr;
                if (File.Exists(filePath))
                {
                    FileStream fs = new FileStream(filePath, FileMode.Append);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(str);
                    //清空缓冲区
                    sw.Flush();
                    //关闭流
                    sw.Close();
                    fs.Close();
                }
                else
                {
                    FileStream fs = new FileStream(filePath, FileMode.Create);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(str);
                    //清空缓冲区
                    sw.Flush();
                    //关闭流
                    sw.Close();
                    fs.Close();
                }
            }
            catch (Exception e)
            {
                LogHelper.Warn("保存药品信息到txt文件出现异常" + e.Message);
            }
        }

        #endregion
        #endregion

        //pivasdrug
        public DelegateCommand PivasDrugs_ComboxNSICCommand { get; set; }

        public void PivasDrugChanged()
        {
            try
            {
                if (SelectedPivasDrug != null)
                {
                    pivasDrugDose = Convert.ToDouble(SelectedPivasDrug.DOSE).ToString();// SelectPivasDrug.DOSE.ToString();
                    //PivasLoadDrug();
                }
            }
            catch (Exception e)
            {
                LogHelper.Warn("更改PIVAS出现异常：" + e.Message);
            }
        }

        //usedDrug
        public DelegateCommand LocalDrugs_ComboxNSICCommand { get; set; }

        public void LocalDrugChanged()
        {
            try
            {
                if (SelectedLocalDrug != null)
                {
                    drugDose = SelectedLocalDrug.DOSE.ToString("G");// SelectPivasDrug.DOSE.ToString();
                }
            }
            catch (Exception e)
            {
                LogHelper.Warn("本地药品改变出现异常：" + e.Message);
            }
        }

        //切换冲刷配药组
        public DelegateCommand FlushGroupIndex_ComboxNSICCommand { get; set; }

        private int OldFlushGroupIndex = 0;

        private void FlushGroupIndexChange()
        {
            try
            {
                if (OldFlushGroupIndex != flushGroupIndex)
                {
                    msDrug.flush_Groups[OldFlushGroupIndex] = msFlushGroup;
                    msFlushGroup = msDrug.flush_Groups[flushGroupIndex];
                    OldFlushGroupIndex = flushGroupIndex;
                    msFlushParam = msFlushGroup.flush_Params[0];
                    pumpBackParam = msFlushGroup.pumpBack_Params[0];
                    OldFlushLoopIndex = 0;
                    flushLoopIndex = 0;
                    OldPumpBackLoopIndex = 0;
                    PumpBackLoopIndex = 0;
                }
            }
            catch (Exception e)
            {
                LogHelper.Warn("配药参数组发生改变出现异常：" + e.Message);
            }
        }

        //切换循环配药Index
        public DelegateCommand FlushLoopIndex_ComboxNSICCommand { get; set; }

        private int OldFlushLoopIndex = 0;

        public void FlushLoopIndexChange()
        {
            try
            {
                if (OldFlushLoopIndex != flushLoopIndex)
                {
                    msFlushGroup.flush_Params[OldFlushLoopIndex] = msFlushParam;
                    msFlushParam = msFlushGroup.flush_Params[flushLoopIndex];
                    OldFlushLoopIndex = flushLoopIndex;
                }
            }
            catch (Exception e)
            {
                LogHelper.Warn("冲刷Index改变出现异常：" + e.Message);
            }
        }

        //切换循环回抽Index
        public DelegateCommand PumpBackLoopIndex_ComboxNSICCommand { get; set; }

        private int OldPumpBackLoopIndex = 0;

        public void PumpBackLoopIndexChange()
        {
            try
            {
                if (OldPumpBackLoopIndex != PumpBackLoopIndex)
                {
                    msFlushGroup.pumpBack_Params[OldPumpBackLoopIndex] = pumpBackParam;
                    pumpBackParam = msFlushGroup.pumpBack_Params[PumpBackLoopIndex];
                    OldPumpBackLoopIndex = PumpBackLoopIndex;
                }
            }
            catch (Exception e)
            {
                LogHelper.Warn("回抽Index 出现异常：" + e.Message);
            }
        }

        //切换药品溶解属性
        public DelegateCommand SOLUBILITY_ComboxNSICCommand { get; set; }

        private void SOLUBILITY_ComboxSIC()
        {
            try
            {
                switch (msDrug.SOLUBILITY)
                {
                    case 0:
                        {
                            msFlushGroup.FlushLoopCount = 1;
                            msFlushGroup.flush_Params[0].DrugCountOfLoopFlush = 4;
                            msFlushParam.DrugCountOfLoopFlush = 4;
                            break;
                        }
                    case 1:
                        {
                            msFlushGroup.FlushLoopCount = 1;
                            msFlushGroup.flush_Params[0].DrugCountOfLoopFlush = 2;
                            msFlushParam.DrugCountOfLoopFlush = 2;
                            break;
                        }
                    case 2:
                        {
                            msFlushGroup.FlushLoopCount = 2;
                            msFlushParam.DrugCountOfLoopFlush = 2;
                            msFlushGroup.flush_Params[0].DrugCountOfLoopFlush = 2;
                            msFlushGroup.flush_Params[1].DrugCountOfLoopFlush = 2;

                            break;
                        }
                    case 3:
                        {
                            msFlushGroup.FlushLoopCount = 1;
                            msFlushParam.DrugCountOfLoopFlush = 1;
                            msFlushGroup.flush_Params[0].DrugCountOfLoopFlush = 1;
                            break;
                        }
                    case 4:
                        {
                            msFlushGroup.FlushLoopCount = 2;
                            msFlushParam.DrugCountOfLoopFlush = 1;
                            msFlushGroup.flush_Params[0].DrugCountOfLoopFlush = 1;
                            msFlushGroup.flush_Params[1].DrugCountOfLoopFlush = 4;
                            break;
                        }
                    case 5:
                        {
                            msFlushGroup.FlushLoopCount = 2;
                            msFlushGroup.flush_Params[0].DrugCountOfLoopFlush = 1;
                            msFlushGroup.flush_Params[1].DrugCountOfLoopFlush = 1;
                            msFlushParam.DrugCountOfLoopFlush = 1;
                            break;
                        }
                    default:
                        {
                            msFlushGroup.FlushLoopCount = 1;
                            msFlushParam.DrugCountOfLoopFlush = 1;
                            break;
                        }
                }
            }
            catch (Exception e)
            {
                LogHelper.Warn("药品溶解属性发生改变， 出现异常：" + e.Message);
            }
        }
    }
}